package com.china.zhuoyue.ui.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.ViewCompat
import com.china.zhuoyue.ui.R
import com.china.zhuoyue.ui.ext.dp
import com.china.zhuoyue.ui.ext.toColor
import com.china.zhuoyue.ui.listener.OnValidationResultListener
import com.china.zhuoyue.ui.utils.VariableViewUtils

class CommonButton : AppCompatTextView, View.OnClickListener, OnValidationResultListener {
    companion object {
        const val CB_NORMAL = 1 // 直接使用主色调非空心
        const val CB_STROKE_TEXT = 2 // 字体色色空心
        const val CB_RED = 3 // 红色字体色色非空心
        const val CB_STROKE_NORMAL = 4 // 正常边框
        const val CB_STROKE_HINT = 5 // 浅色边框
        const val CB_AUTH = 6 // 认证
        const val CB_TAG = 7 // 标签
    }

    private var mOnClickListener: OnClickListener? = null
    private var defaultEnable = true// 是否活跃
    private var mVariableViewUtils: VariableViewUtils? = null
    private var normalDrawable: Drawable? = null
    private var enableFalseDrawable: Drawable? = null
    private var mCBStyle = CB_NORMAL
    private var radius = 30.dp.toFloat()
    private var stroke = 1.dp
    private var textSize = 16.dp

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        setOnClickListener(this)
        init(attrs)
    }

    fun init(attrs: AttributeSet?) {
        attrs?.let {
            val typedArray: TypedArray =
                context.obtainStyledAttributes(attrs, R.styleable.CommonButton)
            mCBStyle = typedArray.getInt(R.styleable.CommonButton_cb_style, CB_NORMAL)
            textSize = typedArray.getDimensionPixelSize(R.styleable.CommonButton_but_text_size, textSize)
            radius = typedArray.getDimension(R.styleable.CommonButton_radius, radius)
            typedArray.recycle()
        }
        changeStyle()
        enableFalseDrawable = GradientDrawable().apply {
            setColor(R.color.text_hint_color.toColor());cornerRadius = radius
        }
        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
        gravity = Gravity.CENTER
    }

    fun changeStyle(cbStyle: Int = mCBStyle) {
        mCBStyle = cbStyle
        when (cbStyle) {
            CB_NORMAL -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setColor(R.color.colorPrimary.toColor());
                }
                setTextColor(R.color.white.toColor())
            }
            CB_RED -> {
                normalDrawable = GradientDrawable().apply {
                    setColor(R.color.button_red.toColor());cornerRadius = radius
                }
                setTextColor(R.color.white.toColor())
            }
            CB_STROKE_TEXT -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.text_color.toColor())
                }
                setTextColor(R.color.text_color.toColor())
            }
            CB_STROKE_NORMAL -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.main_color.toColor())
                }
                setTextColor(R.color.main_color.toColor())
            }
            CB_STROKE_HINT -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.text_login_hint.toColor())
                }
                setTextColor(R.color.text_login_hint.toColor())
            }
            CB_AUTH -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setColor(R.color.text_tag.toColor());
                }
                setTextColor(R.color.text_hint_color_2.toColor())
            }
            CB_TAG -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setColor(R.color.text_tag.toColor());
                }
                setTextColor(R.color.text_auth.toColor())
            }
            /* CB_BG -> {
                 normalDrawable = GradientDrawable().apply { setColor(R.color.bg.toColor());cornerRadius = radius }
                 setTextColor(R.color.text_hint_color_2.toColor())
             }
             CB_MAIN_LIGHT -> {
                 normalDrawable = GradientDrawable().apply { setColor(R.color.main_color_light.toColor());cornerRadius = radius }
                 setTextColor(R.color.main_color.toColor())
             }
             CB_STROKE_TEXT -> {
                 normalDrawable = GradientDrawable().apply {
                     cornerRadius = radius
                     setStroke(stroke, R.color.text_color.toColor())
                 }
                 setTextColor(R.color.text_color.toColor())
             }
             CB_BLUE -> {
                 normalDrawable = GradientDrawable().apply { setColor(R.color.blue.toColor());cornerRadius = radius }
                 setTextColor(R.color.white.toColor())
             }*/
        }
        ViewCompat.setBackground(this, if (defaultEnable) normalDrawable else enableFalseDrawable)
    }

    fun getCbStyle() = mCBStyle

    override fun setOnClickListener(l: OnClickListener?) {
        if (this == l)
            super.setOnClickListener(this)
        else
            mOnClickListener = l
    }

    fun setVariableButtonUtils(variableViewUtils: VariableViewUtils) {
        mVariableViewUtils = variableViewUtils
    }

    override fun onClick(v: View?) {
        if (mOnClickListener != null && defaultEnable) {
            //if (BLUtils.notRepeatClick()) {
            if (mVariableViewUtils == null) {
                mOnClickListener?.onClick(v)
            } else if (mVariableViewUtils!!.clickValidator()) {
                mOnClickListener?.onClick(v)
            }
            //}
        }
    }

    override fun enable(enable: Boolean): Boolean {
        if (enable && !defaultEnable) ViewCompat.setBackground(this, normalDrawable)
        if (!enable && defaultEnable) ViewCompat.setBackground(this, enableFalseDrawable)
        defaultEnable = enable
        return defaultEnable
    }
}